package com.yong.common.date; 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class DateUtil {
	private static final SimpleDateFormat numberDatePattern = new SimpleDateFormat("yyyyMMddHHmmss");
	private static final SimpleDateFormat shortDatePattern = new SimpleDateFormat("MM/dd HH:mm");
	private static final SimpleDateFormat defaultDatePattern = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	private static final SimpleDateFormat commonDatePattern = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	private static final SimpleDateFormat dayPattern = new SimpleDateFormat("yyyy-MM-dd");
	private static final SimpleDateFormat hourPattern = new SimpleDateFormat("yyyy-MM-dd HH");
	private static final SimpleDateFormat logHourPattern = new SimpleDateFormat("yyyy-MM-dd-HH");
	
	//生日中的年月日
	public static List<String> years;
	public static List<String> months;
	public static List<String> days;
	
	public static long getTimeToNum() {
    	String time = numberDatePattern.format(new Date());
    	return Long.valueOf(time);
    }
    
    public static Date getNumToTime(long date) {
    	try {
			return numberDatePattern.parse(Long.toString(date));
		} catch (ParseException e) {
			return null;
		}
    }
    
    public static String getDateFromSystem(long date) {
    	Date aDate = new Date(date);
    	return commonDatePattern.format(aDate);
    }
    
    public static String getDateString(long date) {
    	Date aDate = getNumToTime(date);
    	return shortDatePattern.format(aDate);
    }
    
    public static String getDateLongToString(long date) {
    	Date aDate = getNumToTime(date);
    	return hourPattern.format(aDate);
    }
    public static String getDateByLong(long date) {
    	Date aDate = new Date(date);
    	return dayPattern.format(aDate);
    }
    
    public static Date convertStringToDate(String dateString) {
    	try {
			return defaultDatePattern.parse(dateString);
		} catch (ParseException e) {
			return null;
		}
    }
    
    public static String getYesterday() {
    	Calendar c = Calendar.getInstance();
    	c.add(Calendar.DATE, -1);
    	return dayPattern.format(c.getTime());
    }
    
    public static String getLastHour() {
    	Calendar c = Calendar.getInstance();
    	c.add(Calendar.HOUR_OF_DAY, -1);
    	return hourPattern.format(c.getTime());
    }
    
	public static String getLastHourString() {
		Calendar c = Calendar.getInstance();
    	c.add(Calendar.HOUR_OF_DAY, -1);
    	return logHourPattern.format(c.getTime());	
	}
    
	public static String getTimeToString() {
    	return numberDatePattern.format(new Date());
    }
	
	public static String formatDate(java.util.Date date, String format) {
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");
		if (!StringUtils.isEmpty(format))
			sdf.applyPattern(format);

		return sdf.format(date);
	}
	
	public static java.util.Date parseDate(String str, String format) {

		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");

		if (!StringUtils.isEmpty(format))
			sdf.applyPattern(format);

		try {
			return sdf.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 得到当日开始时间
	 * 
	 * @return
	 */
	public static Date getCurrDayStartTime() {
		Calendar calendar = new GregorianCalendar();
		calendar.set(Calendar.HOUR, -12);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}
	
	/**
	 * 得到当日结束时间
	 * @return
	 */
	public static Date getCurrDayEndTime() {
		Calendar calendar = new GregorianCalendar();
		calendar.set(Calendar.HOUR, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}
	/**
	 * 得到指定日期结束时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getCurrDayEndTime(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}

	/**
	 * 得到当前周一开始时间
	 * 
	 * @return
	 */
	public static Date getCurrWeekFirstDay() {
		Calendar calendar = new GregorianCalendar();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
		calendar.set(Calendar.HOUR, -12);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}
	
	/**
	 * 得到当前周末时间结束时间
	 * 
	 * @return
	 */
	public static Date getCurrWeekLastDay() {
		Calendar calendar = new GregorianCalendar();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() + 6);
		calendar.set(Calendar.HOUR, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);

		return calendar.getTime();
	}
	
	/**
	 * 得到本月的第一天开始时间
	 * 
	 * @return
	 */
	public static Date getCurrMonthFirstDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar
				.getActualMinimum(Calendar.DAY_OF_MONTH));
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);

		return calendar.getTime();
	}

	/**
	 * 得到本月的最后一天结束时间
	 * 
	 * @return
	 */
	public static Date getCurrMonthLastDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar
				.getActualMaximum(Calendar.DAY_OF_MONTH));
		calendar.set(Calendar.HOUR, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}


	/**
	 * 取得某天所在周的第一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfWeek(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
		return c.getTime();
	}

	/**
	 * 取得某天所在周的最后一天
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfWeek(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
		return c.getTime();
	}

	/**
	 * 得到本月的第一天开始时间
	 * 
	 * @return
	 */
	public static Date getFirstDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar
				.getActualMinimum(Calendar.DAY_OF_MONTH));
		calendar.setTime(date);

		return calendar.getTime();
	}

	/**
	 * 得到本月的最后一天结束时间
	 * 
	 * @return
	 */
	public static Date getLastDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar
				.getActualMaximum(Calendar.DAY_OF_MONTH));
		calendar.setTime(date);
		return calendar.getTime();
	}
	
	/**
	 * 得到给定的一个时间与当前相比间隔的时间，返回值 eg: 6小时前，10天前
	 * 
	 * @param oldTime
	 *            long 类型时间
	 * @return
	 */
	@Deprecated
	public static String getTimeStrBetween(long oldTime) {
		Calendar calendar = Calendar.getInstance();
		Calendar oldCalendar = Calendar.getInstance();

		if (calendar.before(oldCalendar)) {
			return null;
		}
		oldCalendar.setTimeInMillis(oldTime);

		int years = calendar.get(java.util.Calendar.YEAR)
				- oldCalendar.get(java.util.Calendar.YEAR);
		if (years > 0)
			return years + "年前";

		int months = calendar.get(java.util.Calendar.MONTH)
				- oldCalendar.get(java.util.Calendar.MONTH);

		if (months > 0)
			return months + "月前";

		int days = calendar.get(java.util.Calendar.DAY_OF_YEAR)
				- oldCalendar.get(java.util.Calendar.DAY_OF_YEAR);
		if (days > 0) {
			return days + "天前";
		}

		int hours = calendar.get(java.util.Calendar.HOUR_OF_DAY)
				- oldCalendar.get(java.util.Calendar.HOUR_OF_DAY);
		if (hours > 0)
			return hours + "小时前";

		int minutes = calendar.get(java.util.Calendar.MINUTE)
				- oldCalendar.get(java.util.Calendar.MINUTE);
		if (minutes > 0)
			return minutes + "分钟前";

		int seconds = calendar.get(java.util.Calendar.SECOND)
				- oldCalendar.get(java.util.Calendar.SECOND);
		return seconds + "秒前";
	}
	
	public static String getTimeStrBetween(long startTime, long endTime) {
		Calendar endCalendar = Calendar.getInstance();
		Calendar startCalendar = Calendar.getInstance();

		endCalendar.setTimeInMillis(Math.max(startTime, endTime));
		startCalendar.setTimeInMillis(Math.min(startTime, endTime));

		int years = endCalendar.get(java.util.Calendar.YEAR)
				- startCalendar.get(java.util.Calendar.YEAR);
		if (years > 0)
			return years + "年";

		int months = endCalendar.get(java.util.Calendar.MONTH)
				- startCalendar.get(java.util.Calendar.MONTH);

		if (months > 0)
			return months + "月";

		int days = endCalendar.get(java.util.Calendar.DAY_OF_YEAR)
				- startCalendar.get(java.util.Calendar.DAY_OF_YEAR);
		if (days > 0) {
			return days + "天";
		}

		int hours = endCalendar.get(java.util.Calendar.HOUR_OF_DAY)
				- startCalendar.get(java.util.Calendar.HOUR_OF_DAY);
		if (hours > 0)
			return hours + "小时";

		int minutes = endCalendar.get(java.util.Calendar.MINUTE)
				- startCalendar.get(java.util.Calendar.MINUTE);
		if (minutes > 0)
			return minutes + "分钟";

		int seconds = endCalendar.get(java.util.Calendar.SECOND)
				- startCalendar.get(java.util.Calendar.SECOND);
		return seconds + "秒";
	}
	
	
	/**
	 * 当前日期减去指定天数
	 * 
	 * @param d
	 *            指定天数,正负数
	 * @return Calendar
	 */
	public static Calendar addDate(int d) {
	    Calendar date = Calendar.getInstance();
	    date.add(Calendar.DATE, d);
	    return date;
	}
	/**
	 * 当前日期减去指定月数
	 * 
	 * @param m
	 *            指定月数,正负数
	 * @return Calendar
	 */
	public static Calendar addMonth(int m) {
	    Calendar date = Calendar.getInstance();
	    date.add(Calendar.MONTH, m);
	    return date;
	}
	
	/**
	 * @return a collection of year from 1970 to now
	 */
	public static List<String> getYears() {
		years = new ArrayList<String>();
		years = new ArrayList<String>();
		int y = Calendar.getInstance().get(Calendar.YEAR);
		for (int i = 1970; i <= y; i++) {
			years.add(String.valueOf(i));
		}
		return years;
	}
	
	public static Map<String, String> getFullYears() {
		Map<String, String> years = new LinkedHashMap<String, String>();
		int y = Calendar.getInstance().get(Calendar.YEAR);
		
		for (int i = y; i >=1970; i--) {
			years.put(String.valueOf(i), String.valueOf(i));
			if (String.valueOf(i).substring(3, 4).equals("0")) {
				years.put(String.valueOf(i).substring(2, 4) + "ee", String.valueOf(i).substring(2, 4) + "后");
			}
		}
		return years;
	}
		
	public static List<String> getJustYears() {
		years = new ArrayList<String>();
		int y = Calendar.getInstance().get(Calendar.YEAR);
		for (int i = y; i <= y + 4; i++) {
			years.add(String.valueOf(i));
		}
		return years;
	}
	
	public static List<String> getYearsReverse() {
		List<String> years = getYears();
		java.util.Collections.reverse(years);
		return years;
	}
	
	/**
	 * @return a collection of months in year
	 */
	public static List<String> getMonths() {
		if (months == null) {
			months = new ArrayList<String>();
			for (int i = 1; i <= 12; i++) {
				months.add(i < 10 ? "0" + i : String.valueOf(i));
			}
		}
		return months;
	}
	
	/**
	 * @return a collection of days in month
	 */
	public static List<String> getDays() {
		if (days == null) {
			days = new ArrayList<String>();
			for (int i = 1; i <= 31; i++) {
				days.add(i < 10 ? "0" + i : String.valueOf(i));
			}
		}
		return days;
	}
	
	public static String getDayInWeek(java.util.Date date) {
		java.util.Calendar c = java.util.Calendar.getInstance();
		c.setTime(date);
		int dayInWeek = c.get(java.util.Calendar.DAY_OF_WEEK);
		String[] weeks = new String[]{
				"日", "一", "二", "三", "四", "五", "六"
		};
		return weeks[dayInWeek - 1];
	}
	public static void main(String[] args) {
		
		
	}
		
}
 